#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <curl/curl.h>
#include <vector>
#include <iterator>
#include <iomanip>

// 将本地图片文件编码为 Base64
std::string encodeBase64(const std::string& filePath) {
    std::ifstream file(filePath, std::ios::binary);
    if (!file.is_open()) {
        std::cerr << "无法打开文件: " << filePath << std::endl;
        return "";
    }

    std::ostringstream oss;
    oss << file.rdbuf();
    std::string fileContents = oss.str();

    static const char* base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    unsigned char const* bytes_to_encode = reinterpret_cast<const unsigned char*>(fileContents.data());
    unsigned int in_len = fileContents.size();
    std::string ret;

    int i = 0;
    int j = 0;
    unsigned char char_array_3[3];
    unsigned char char_array_4[4];

    while (in_len--) {
        char_array_3[i++] = *(bytes_to_encode++);
        if (i == 3) {
            char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
            char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
            char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
            char_array_4[3] = char_array_3[2] & 0x3f;

            for (i = 0; i < 4; i++)
                ret += base64_chars[char_array_4[i]];
            i = 0;
        }
    }

    if (i) {
        for (j = i; j < 3; j++)
            char_array_3[j] = '\0';

        char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
        char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
        char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
        char_array_4[3] = char_array_3[2] & 0x3f;

        for (j = 0; j < i + 1; j++)
            ret += base64_chars[char_array_4[j]];

        while (i++ < 3)
            ret += '=';
    }

    return ret;
}

// 使用 libcurl 发送邮件
void sendEmail(const std::string& from, const std::string& to, const std::string& subject, const std::string& body, const std::string& base64_image) {
    CURL* curl;
    CURLcode res = CURLE_OK;

    std::string payload_text =
        "To: " + to + "\r\n"
        "From: " + from + "\r\n"
        "Subject: " + subject + "\r\n"
        "MIME-Version: 1.0\r\n"
        "Content-Type: multipart/alternative; boundary=\"boundary\"\r\n"
        "\r\n"
        "--boundary\r\n"
        "Content-Type: text/html; charset=\"utf-8\"\r\n"
        "\r\n"
        "<html><body>"
        "<p>" + body + "</p>"
        "<p>谭智鹏 | 教学部 | 长沙中心</p>"
        "<img src=\"data:image/png;base64," + base64_image + "\" width=\"100\"><br>"
        "<p>公司地址：长沙市岳麓区旺龙路辰泰科技园D座3楼华清远见<br>"
        "手机号码：15073403740<br>电话号码：0731-8620 2982<br>"
        "咨询热线：400-611-6270<br>电子邮件：tanzp_cs@hqyj.com</p>"
        "</body></html>\r\n"
        "--boundary--";

    struct curl_slist* recipients = NULL;
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_USERNAME, "tanzp_cs@hqyj.com");  // 发件人邮箱
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "TmbMXJVMcG76HQDs");  // 邮箱授权码
        curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.qiye.163.com:465");  // SMTP 服务器

        curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from.c_str());
        recipients = curl_slist_append(recipients, to.c_str());
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, [](char* buffer, size_t size, size_t nitems, void* userdata) -> size_t {
            std::string* text = reinterpret_cast<std::string*>(userdata);
            size_t buffer_size = size * nitems;
            if (text->size() > buffer_size) {
                text->copy(buffer, buffer_size);
                text->erase(0, buffer_size);
                return buffer_size;
            }
            else {
                size_t len = text->size();
                text->copy(buffer, len);
                text->clear();
                return len;
            }
        });
        curl_easy_setopt(curl, CURLOPT_READDATA, &payload_text);
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

        res = curl_easy_perform(curl);
        if (res != CURLE_OK)
            fprintf(stderr, "邮件发送失败: %s\n", curl_easy_strerror(res));

        curl_slist_free_all(recipients);
        curl_easy_cleanup(curl);
    }
}

int main() {
    std::string from = "tanzp_cs@hqyj.com";
    std::string to = "caot_cs@hqyj.com";
    std::string subject = "测试邮件";
    std::string body = "这是测试邮件的内容。";

    // 编码本地图片
    std::string base64_image = encodeBase64("hqyj_logo.png");  // 请确保图片文件位于脚本同目录中

    // 发送邮件
    sendEmail(from, to, subject, body, base64_image);

    return 0;
}



